package dataAccess;

import java.util.Calendar;

import logic.Ticket;
import logic.Ticket.State;
import exception.MyException;

public class TicketDB {
	
	public void save(Ticket t, double rate) throws MyException {	
		int state = 0 ;
		DBManager db;
		try {
			db = new DBManager();
		} catch (MyException e1) {
			throw e1;
		}	
		try{
			if (t.getState().equals(State.active)){
				state = 1;			
			}
			Calendar aux = t.getFrom();
			//Aclaracion: aux.get(Calendar.MONTH) es el mes de 0 a 11
			String from = aux.get(Calendar.YEAR)+"-"+ aux.get(Calendar.MONTH)+"-"+aux.get(Calendar.DAY_OF_MONTH)+" "+aux.get(Calendar.HOUR_OF_DAY)+":"+aux.get(Calendar.MINUTE)+":00.0";
			Calendar auxSale = t.getSaleDate();
			String saleDate = auxSale.get(Calendar.YEAR)+"-"+ auxSale.get(Calendar.MONTH)+"-"+auxSale.get(Calendar.DAY_OF_MONTH);
			db.execute("insert into agency.tickets (idTickets,fromDate,duration,valueRate,saleDate,state,plate) "+
					"values ("+t.getIdTicket()+
					",'"+from+"'"+
					","+t.getDuration()+
					","+rate+
					",'"+saleDate+"'"+
					","+state+
					",'"+t.getPlate()+"'"+")" );			
		}catch (MyException e) {
			throw e;
		}finally{
			db.close();
		}
	}
}
